package es.christiangarcia.android.pokepedia.dao;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import es.christiangarcia.android.pokepedia.dao.provider.PokepediaDatabaseHelper;
import es.christiangarcia.android.pokepedia.model.db.PokemonTable;
import es.christiangarcia.android.pokepedia.model.vo.PokemonVO;

public class PokemonDAO {

	// Database fields
	private PokepediaDatabaseHelper dbHelper;
	private Context context;

	public PokemonDAO(Context context) {
		this.dbHelper = new PokepediaDatabaseHelper(context);
		this.openDatabase();

	}

	public void openDatabase() {
		try {
			this.dbHelper.createDataBase();
		} catch (IOException ioe) {
			throw new Error("Unable to create database");
		}

		try {
			this.dbHelper.openDataBase();
		} catch (SQLException sqle) {
			throw sqle;
		}
	}

	public void closeDatabase() {
		dbHelper.close();
	}

	public Context getContext() {
		return context;
	}

	public PokemonVO createPokemon(PokemonVO pokemon) {
		ContentValues values = pokemonVOToContentValues(pokemon);

		long insertId = dbHelper.getDatabase().insert(PokemonTable.TABLE_NAME, null, values);
		// To show how to query
		Cursor cursor = dbHelper.getDatabase().query(PokemonTable.TABLE_NAME, PokemonTable.ALL_COLUMNS,
				PokemonTable._ID + " = " + insertId, null, null, null, null);
		cursor.moveToFirst();
		return cursorToPokemonVO(cursor);
	}

	public void deletePokemonVO(PokemonVO pokemon) {
		long id = pokemon.getId();
		System.out.println("PokemonVO deleted with id: " + id);
		dbHelper.getDatabase().delete(PokemonTable.TABLE_NAME, PokemonTable._ID + " = " + id, null);
	}

	public List<PokemonVO> getAllPokemonVOs() {
		List<PokemonVO> pokemons = new ArrayList<PokemonVO>();
		Cursor cursor = dbHelper.getDatabase().query(PokemonTable.TABLE_NAME, PokemonTable.ALL_COLUMNS, null, null,
				null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			PokemonVO pokemon = cursorToPokemonVO(cursor);
			pokemons.add(pokemon);
			cursor.moveToNext();
		}
		cursor.close();
		return pokemons;
	}

	private ContentValues pokemonVOToContentValues(PokemonVO pokemon) {
		ContentValues values = new ContentValues();
		values.put(PokemonTable._ID, pokemon.getId());
		values.put(PokemonTable.NUM_COLUMN, pokemon.getNum());
		values.put(PokemonTable.NAME_COLUMN, pokemon.getName());
		values.put(PokemonTable.NUM_PREV_EVOL_COLUMN, pokemon.getPreEvolutionId());
		values.put(PokemonTable.TYPE_FIRST_COLUMN, pokemon.getTypeFirst());
		values.put(PokemonTable.TYPE_SECOND_COLUMN, pokemon.getTypeSecond());
		return values;
	}

	private PokemonVO cursorToPokemonVO(Cursor cursor) {
		PokemonVO pokemon = new PokemonVO();
		pokemon.setId(cursor.getInt(cursor.getColumnIndex(PokemonTable._ID)));
		pokemon.setNum(cursor.getInt(cursor.getColumnIndex(PokemonTable.NUM_COLUMN)));
		pokemon.setName(cursor.getString(cursor.getColumnIndex(PokemonTable.NAME_COLUMN)));
		pokemon.setPreEvolutionId(cursor.getInt(cursor.getColumnIndex(PokemonTable.NUM_PREV_EVOL_COLUMN)));
		pokemon.setTypeFirst(cursor.getString(cursor.getColumnIndex(PokemonTable.TYPE_FIRST_COLUMN)));
		pokemon.setTypeSecond(cursor.getString(cursor.getColumnIndex(PokemonTable.TYPE_SECOND_COLUMN)));
		return pokemon;
	}
}
